﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessLogic;

namespace Interface
{
    public partial class Search : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        DataValid dv = new DataValid();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                loadCity();
                loadCategory();
            }
            

        }

        protected void loadCity()
        {
            string strsql = "Select Name from City";
            DataTable dt = dc.getData(strsql);
            ddlCity.Items.Add(string.Empty);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    string CityName = row["Name"].ToString();

                    ddlCity.Items.Add(CityName);
                }
            }

        }

        protected void loadDis(string _idCity)
        {

            string strsql = "Select Name from District where idCity = '" + _idCity + "'";
            DataTable dt = dc.getData(strsql);


            if (dt.Rows.Count > 0)
            {
                ddlDis.Items.Add(string.Empty);
                foreach (DataRow row in dt.Rows)
                {
                    string _DisName = row["Name"].ToString();

                    ddlDis.Items.Add(_DisName);
                }
            }
            


        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDis.Items.Clear();
            string sqlIdcity = "Select IdCity from City where Name = N'" + ddlCity.Text + "'";
            int _idCity = dc.getNumberValue(sqlIdcity);

            loadDis(_idCity.ToString());
        }

        protected void loadCategory()
        {
            string strsql = "Select Name from Category";
            DataTable dt = dc.getData(strsql);
            ddlCategory.Items.Add(string.Empty);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    string CategoryName = row["Name"].ToString();

                    ddlCategory.Items.Add(CategoryName);
                }
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (ddlCity.Text != string.Empty && ddlDis.Text == string.Empty && ddlCategory.Text == string.Empty && ddlPrice.Text == string.Empty)
            {
                string CityName = ddlCity.Text;
                string sqlIdcity = "select IdCity from City where Name= N'" + CityName + "'";
                int cityId = dc.getNumberValue(sqlIdcity);
                Response.Redirect("~/Houses/BoardingHouse.aspx?CityID=" + cityId);
            }

            if(ddlCity.Text != string.Empty && ddlDis.Text != string.Empty && ddlCategory.Text == string.Empty && ddlPrice.Text == string.Empty)
            {
                string CityName = ddlCity.Text;
                string sqlIdcity = "select IdCity from City where Name= N'" + CityName + "'";
                int cityId = dc.getNumberValue(sqlIdcity);

                string DisName =ddlDis.Text;
                string sqlIdDis = "select IdDistrict from District where Name = N'"+ DisName  +"'";
                int disId = dc.getNumberValue(sqlIdDis);

                Response.Redirect("~/Houses/BoardingHouse.aspx?CityID=" + cityId + "&DisID=" + disId);
            }

            if (ddlCity.Text != string.Empty && ddlDis.Text != string.Empty && ddlCategory.Text != string.Empty && ddlPrice.Text == string.Empty)
            {
                string CityName = ddlCity.Text;
                string sqlIdcity = "select IdCity from City where Name= N'" + CityName + "'";
                int cityId = dc.getNumberValue(sqlIdcity);

                string DisName = ddlDis.Text;
                string sqlIdDis = "select IdDistrict from District where Name = N'" + DisName + "'";
                int disId = dc.getNumberValue(sqlIdDis);

                string CateName = ddlCategory.Text;
                string sqlIdCate = "select IdCategory from Category where Name = N'" + CateName + "'";
                int CateId = dc.getNumberValue(sqlIdCate);

                Response.Redirect("~/Houses/BoardingHouse.aspx?CityID=" + cityId + "&DisID=" + disId + "&CateID=" + CateId);
 
            }


            //if (ddlCity.Text != string.Empty && ddlDis.Text != string.Empty && ddlCategory.Text != string.Empty && ddlPrice.Text != string.Empty)
            //{
            //    string CityName = ddlCity.Text;
            //    string sqlIdcity = "select IdCity from City where Name= N'" + CityName + "'";
            //    int cityId = dc.getNumberValue(sqlIdcity);

            //    string DisName = ddlDis.Text;
            //    string sqlIdDis = "select IdDistrict from District where Name = N'" + DisName + "'";
            //    int disId = dc.getNumberValue(sqlIdDis);

            //    string CateName = ddlCategory.Text;
            //    string sqlIdCate = "select IdCategory from Category where Name = N'" + CateName + "'";
            //    int CateId = dc.getNumberValue(sqlIdCate);


            //    int max = 0;
            //    int min = 0;
            //    string Price = ddlPrice.Text.ToString();
            //    if (Price == "Nhỏ Hơn 500 ngàn")
            //    {
            //        max = 500000;
                    
            //    }
            //    else if (Price == "Từ 500 ngàn - 1tr")
            //    {
            //        max = 1000000;
            //        min = 500000;
            //    }
            //    else
            //    {
            //        min = 1000000;                  
            //    }

            //    Response.Redirect("~/Houses/BoardingHouse.aspx?CityID=" + cityId + "&DisID=" + disId + "&CateID=" + CateId + "&Min=" + min + "&Max=" + max);
            //}
        }
    }
}